<template>
    <div class="open-trades">
        <div class="title">Open Trades</div>
        <div class="table">
            <table>
                <thead>
                    <tr>
                        <th>SYMBOL</th>
                        <th>NAME</th>
                        <th>TYPE</th>
                        <th>QUANTITY</th>
                        <th>PRICE</th>
                        <th>MARKETCAP</th>
                        <th>FEE</th>
                        <th>TOTALAMOUNT</th>
                    </tr>
                </thead>
                <tbody v-if="open_list.length">
                    <tr v-for="(item, index) in open_list" :key="item.id">
                        <td>{{ item.shortName }}</td>
                        <td>{{ item.fullName }}</td>
                        <td :style="`color: ${ typeMap[item.openSide].color }`">{{ typeMap[item.openSide].text }}</td>
                        <td>{{ item.quantity }}</td>
                        <td>${{ item.openPrice }}</td>
                        <td>${{ item.marketCap }}</td>
                        <td>${{ item.fee }}</td>
                        <td>${{ item.totalAmount }}</td>
                    </tr>
                </tbody>
                <tbody v-else>
                    <tr class="empty-tr">
                        <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import { assetService } from '@/service/asset';

const typeMap = {
    0: {
        text: 'Buy',
        color: 'var(--positive-number-color)'
    },
    1: {
        text: 'Sell',
        color: 'var(--negative-number-color)'
    },
};

// 开仓记录列表
const open_list = ref([]);

onMounted(() => {
    // 获取开仓记录列表
    assetService.getStockOpenList(open_list);
});
</script>

<style scoped lang="less">
.open-trades {
    box-sizing: border-box;
    width: 100%;
    padding: 1.5vw;
    display: flex;
    flex-direction: column;
    gap: 1vw;
    background-color: #fff;
    box-shadow: 0 0 0.5vw rgba(0, 0, 0, 0.05);
    border-radius: 0.5vw;

    .title {
        font-size: 1.1vw;
        font-weight: 600;
        color: var(--text-color);
    }

    .table {
        width: 100%;
        
        table {
            width: 100%;
            border-collapse: collapse;

            thead {
                height: 3vw;
                background-color: var(--bg-color);

                th {
                    padding: 1vw;
                    font-size: 0.8vw;
                    text-align: left;
                    font-weight: 500;
                    color: var(--text-light-color);
                }
            }

            tbody {
                min-height: 30vw;

                tr {
                    height: 3vw;
                    border-top: 0.1vw solid var(--border-color);
                    
                    &:hover {
                        background-color: var(--bg-light-color);
                    }

                    td {
                        padding: 1vw;
                        font-size: 0.7vw;
                        text-align: left;
                        color: var(--text-color);
                    }

                    .btn-group {
                        display: flex;
                        align-items: center;
                        gap: 0.5vw;

                        .btn {
                            padding: 0.3vw 0.8vw;
                            border-radius: 0.5vw;
                            color: #fff;
                            cursor: pointer;
                        }
                        
                        .buy-btn {
                            background-color: var(--active-color);
                        }

                        .sell-btn {
                            background-color: var(--negative-number-color);
                        }
                    }
                }

                .empty-tr {
                    --h: 10vw;
                    width: 100%;
                    height: var(--h);
                    position: relative;

                    &::after {
                        content: "No Data";
                        font-size: 1vw;
                        color: var(--text-light-color);
                        display: block;
                        width: 100%;
                        height: var(--h);
                        position: absolute;
                        top: 0;
                        left: 0;
                        text-align: center;
                        line-height: var(--h);
                    }
                }
            }
        }
    }
}
</style>